package com.yilin.tms.order.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.utils.UserUtil;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.data.statistics.order.goods.GoodsStatistics;
import com.yilin.tms.core.entity.data.statistics.OrderStatistics;
import com.yilin.tms.core.entity.data.statistics.order.rob.RobStatistics;
import com.yilin.tms.core.entity.data.statistics.order.transit.TransitStatistics;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.order.service.statistics.IOrderStatisticsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order/statistics/")
public class StatisticsController extends BaseController {

    @Resource
    private IOrderStatisticsService statisticsService;
    /**
     * 获取运单统计数据
     */
    @PostMapping("getTransitStatistics")
    public ReturnData<TransitStatistics> getTransitStatistics() {
        TransitStatistics transitStatistics = statisticsService.getTransitStatistics(getLoginUser());
        return jsonView(true, "获取成功", transitStatistics);
    }

    /**
     * 获取抢单统计数据
     */
    @PostMapping("getRobStatistics")
    public ReturnData<RobStatistics> getRobStatistics() {
        RobStatistics robStatistics = statisticsService.getRobStatistics(getLoginUser());
        return jsonView(true, "获取成功", robStatistics);
    }

    /**
     * 获取货源统计数据
     */
    @PostMapping("getGoodsStatistics")
    public ReturnData<GoodsStatistics> getGoodsStatistics() {
        GoodsStatistics goodsStatistics = statisticsService.getGoodsStatistics(getLoginUser());
        return jsonView(true, "获取成功", goodsStatistics);
    }

    /**
     * 获取运单流程模块 统计数据
     */
    @PostMapping(value = "getOrderStatistics")
    public ReturnData<OrderStatistics> getOrderStatistics() {
        OrderStatistics statistics = new OrderStatistics();
        statistics.setClearingDetail(statisticsService.getClearingCount());
        statistics.setGatherRecord(statisticsService.getGatherRecordCount());
        statistics.setOrderPaymentCost(statisticsService.getOrderPaymentCostCount());
        statistics.setOrderRefundCost(statisticsService.getOrderRefundCostCount());
        statistics.setOrderSettlementCost(statisticsService.getOrderSettlementCostCount());
        if (statistics.getClearingDetail() == 0 && statistics.getGatherRecord() == 0){
            statistics.setAllEmpty(true);
        }
        return jsonView(SUCCESS_CODE, "获取成功",statistics);
    }

    /**
     * 根据组织ID查询前十天的抢单量
     */
    @PostMapping(value = "getTradingTrendData")
    @NotNull("organizeId")
    public ReturnData<List<Map<String, Object>>> getTradingTrendData(@Explain("组织ID") String organizeId) {
        List<Map<String, Object>> tradingTrendData = statisticsService.getTradingTrendData(organizeId);
        return jsonView(true, "获取抢单成功", tradingTrendData);
    }
}
